$(function(){
    waiting.init();
});

var waiting = {
    init: function(){
        var waiting = $("<div>").addClass("waiting").hide();
        var middle = $("<div>").addClass("middle");
        for (var i = 1; i <= 9; i++) {
            middle.append("<div></div>");
        }
        $("body").append(waiting.append(middle));
        setMiddleBlock();
    },
    showLoading: function(){
        $("div.waiting").show();
    },
    hideWaiting: function () {
        $("div.waiting").hide();
    }
};

var ajax = {
    errMsg: "网络连接错误，请稍后重试",
    count: 0,  //正在执行的ajax数量
    defaultOnError: function(errMsg) {
        Lobibox.notify("error",{title: "错误", msg:errMsg});
    },
    // 基本的ajax方法封装
    doAjax: function (options) {
        var onSuccess = options.success;
        var onComplete = options.complete;
        var onError = options.error;

        options.beforeSend = function(xhr) {
            if (ajax.count === 0) {
                waiting.showLoading();
            }
            ajax.count = ajax.count + 1;
        };
        options.success = function(data, status, xhr){
            onSuccess(data, status, xhr);
        };
        options.error = function(xhr, textStatus, errorThrown){
            onError(xhr, textStatus, errorThrown);
        };
        options.complete = function(event,xhr,options){
            ajax.count = ajax.count - 1;
            if (ajax.count === 0) {
                waiting.hideWaiting();
            }
            if (onComplete !== undefined) {
                onComplete(event,xhr,options);
            }
        };

        $.ajax(options);
    }
};

